package com.zjj.springstu.use.coreConcept.beanDefinition;

import com.zjj.springstu.springHandwriting.app.service.UserService;
import com.zjj.springstu.use.coreConcept.config.AppConfig;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author zhanglei.zjj
 * @description BeanDefinition学习
 * 我们可以编程式手动注册BeanDefinition，这样也可以实现将Bean对象交给Spring容器管理
 *
 * @date 2023/4/11 22:55
 */
public class TestBeanDefinition {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // 生成一个BeanDefinition对象, 并设置 beanClass，然后注册到Spring容器中
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition.setBeanClass(UserService.class);
        context.registerBeanDefinition("userService", beanDefinition);

        System.out.println(context.getBean("userService"));
    }
}
